'use strict'
const path = require('path')
const { defineConfig } = require('@vue/cli-service')


function resolve(dir) {
  return path.join(__dirname, dir)
}

const name = process.env.VUE_APP_TITLE || '日霄CMS系统' // 网页标题
const port = process.env.port || process.env.npm_config_port || 12545 // 端口

module.exports = defineConfig({
  // 部署路径配置
  publicPath: process.env.NODE_ENV === "production" ? "/cms/" : "/",
  outputDir: 'dist',
  assetsDir: 'static',
  
  // 开发配置
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  transpileDependencies: ['quill'],

  // 开发服务器配置
  devServer: {
    host: '0.0.0.0',
    port: port,
    open: true,
    proxy: {
      [process.env.VUE_APP_BASE_API || '/api']: {
        target: `http://localhost:12543`, // CMS后端API地址
        changeOrigin: true,
        pathRewrite: {
          ['^' + (process.env.VUE_APP_BASE_API || '/api')]: ''
        }
      },
      '/static': {  // CMS静态资源代理
        target: `http://localhost:12543`,
        changeOrigin: true
      },
      '/profile': {  // 新增对/profile路径的代理
        target: `http://localhost:12543`,
        changeOrigin: true
      }
    },
    client: {
      overlay: {
        warnings: false,
        errors: true
      }
    },
   
  },

  // CSS 配置
  css: {
    extract: true, // 生产环境提取CSS
    sourceMap: false,
    loaderOptions: {
      sass: {
        sassOptions: { 
          outputStyle: "expanded",
          // 全局注入变量和混合
          additionalData: `@import "@/styles/variables.scss"; @import "@/styles/mixins.scss";`
        }
      }
    }
  },

  // Webpack 配置
  configureWebpack: {
    name: name,
    resolve: {
      alias: {
        '@': resolve('src'),
        '@components': resolve('src/components'),
        '@views': resolve('src/views'),
        '@assets': resolve('src/assets')
      }
    },
    plugins: [
      
    ],
    performance: {
      hints: 'warning',
      maxAssetSize: 2 * 1024 * 1024, // 2MB
      maxEntrypointSize: 2 * 1024 * 1024 // 2MB
    }
  },

  // 链式配置
  chainWebpack(config) {
    // 移除预加载
    config.plugins.delete('preload')
    config.plugins.delete('prefetch')

    // SVG雪碧图配置
    config.module
      .rule('svg')
      .exclude.add(resolve('src/assets/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/assets/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

   

    // 生产环境优化
    config.when(process.env.NODE_ENV === 'production', config => {
      config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial'
          },
          elementUI: {
            name: 'chunk-elementUI',
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/,
            priority: 20
          },
          commons: {
            name: 'chunk-commons',
            test: resolve('src/components'),
            minChunks: 3,
            priority: 5,
            reuseExistingChunk: true
          }
        }
      })
      
      config.optimization.runtimeChunk('single')
      
      config
        .plugin('ScriptExtHtmlWebpackPlugin')
        .after('html')
        .use('script-ext-html-webpack-plugin', [{
          inline: /runtime\..*\.js$/
        }])
        .end()
    })
  },

  // 插件选项
  pluginOptions: {
    'style-resources-loader': {
      preProcessor: 'scss',
      patterns: [
        resolve('src/styles/variables.scss'),
        resolve('src/styles/mixins.scss')
      ]
    }
  }
})